import { getArticle, delArticle, updataArticle,ssArticle } from "../api/test"
import { Rspones,Payload ,Item} from "../typings"
import { message } from 'antd'
export default {
    //仓库名
    namespace: "pagList",
    state: {
        allLIst: []
    },
    //同步
    reducers: {
        //获取数据
        setList(state: any, { payload }: Payload) {
            state.allLIst = payload.allLIst.data.data[0]
        },
        //删除数据
        delList(state: any, { payload }: Payload) {
            state.allLIst.forEach((item: Item, index: number) => {
                if (item.id == payload.id) {
                    state.allLIst.splice(index, 1)
                }
            })
            message.success('删除成功');
        },
        //编辑
        updataList(state: any, { payload }: Payload) {
            state.allLIst.forEach((item: Item, index: number) => {
                if (item.id == payload.id) {
                    state.allLIst[index].status = payload.status
                }
            })
        },
        //搜索
        ssData(state: any, { payload }: Payload){
            state.allLIst = payload.allLIst.data.data
        }
    },
    //异步
    effects: {
        //异步获取数据
        *getList(action: any, { call, put }: any) {
            let result: Rspones = yield call(getArticle)
            yield put({ type: "setList", payload: { allLIst: result } })
        },
        //异步删除数据
        *delListData({ payload }: Payload, { call, put }: any) {
            let result: Rspones = yield call(delArticle, payload.id)
            yield put({ type: "delList", payload: { allLIst: result, id: payload.id } })
        },
        //异步编辑
        *updataData({ payload }: Payload, { call, put }: any) {
            let result: Rspones = yield call(updataArticle, payload.id, payload.status)
            yield put({ type: "updataList", payload: { allLIst: result, id: payload.id, status: payload.status } })
        },
        //异步搜索
        *ssList({ payload }: Payload, { call, put }: any){
            let result: Rspones = yield call(ssArticle, payload.page, payload.pageSize, payload.name, payload.path, payload.status)
            yield put({ type: "setList", payload: { allLIst: result } })
        }
    }
}
